package com.jollychic.controller;

import com.jollychic.exception.JollyExecption;
import com.jollychic.model.User;
import com.jollychic.services.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

/**
 * Created by xusheng on 2019/4/18.
 */

@RestController //Rest控制器
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService userService;

    @RequestMapping("/getUserName/{userId}")
    @ResponseBody
    public User getUserName(@PathVariable() Integer userId){
        User user=new User();
        user.setUserId(userId);
        user.setUserName("xusheng");
        try {
            int i = 10 / 0;
        }
        catch (Exception e){
            throw new JollyExecption("除数不能为0","1002");
        }
        return user;
    }

    @RequestMapping("/addUserMaster/{userName}")
    @ResponseBody
    public User addUserMaster(@PathVariable() String userName){
        User user= new User();
        user.setUserName(userName);
        user.setUserId(new Random().nextInt(100));
        return userService.addUserMaster(user);
    }

    @RequestMapping("/addUserSlave/{userName}")
    @ResponseBody
    public User addUserSlave(@PathVariable() String userName){
        User user= new User();
        user.setUserName(userName);
        user.setUserId(new Random().nextInt(100));
        return userService.addUserSlave(user);
    }

    @RequestMapping("/addUserTran/{userName}")
    @ResponseBody
    public User addUserTran(@PathVariable() String userName){
        User user= new User();
        user.setUserName(userName);
        user.setUserId(new Random().nextInt(100));
        return userService.addUserTran(user);
    }


//    @RequestMapping("/addUser/{userName}")
//    @ResponseBody
//    public User addUser(@PathVariable() String userName){
//        User user= new User();
//        user.setUserName(userName);
//        user.setUserId(new Random().nextInt(100));
//        return userService.addUser(user);
//    }


}
